<template>
    
  <div class="login">
        
    <div class="mui-content">
            
      <form class="mui-input-group">
                
        <div class="mui-input-row">
                    <label>账号</label>           <input
            v-model="loginForm.username"
            type="text"
            class="mui-input-clear mui-input"
            placeholder="请输入账号"
          />
                  
        </div>
                
        <div class="mui-input-row">
                    <label>密码</label>           <input
            v-model="loginForm.password"
            type="password"
            class="mui-input-clear mui-input"
            placeholder="请输入密码"
          />
                  
        </div>
              
      </form>
            
      <div class="mui-content-padded">
                <button
          @click="login"
          type="button"
          class="mui-btn mui-btn-block mui-btn-primary"
        >
          登录
        </button>
                
        <div class="link-area">
          <a @click="register">还没有账号？前往注册</a>
        </div>
              
      </div>
          
    </div>
      
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    login() {
      //登录操作
      //1.判断用户名和密码是否输入
      if (this.loginForm.username === "" || this.loginForm.password === "") {
        this.$toast("账号或密码不能为空");
      } else {
        //2.发送请求进行登录，请求方法是post，请求地址是login，发送数据是loginForm
        this.$http
          .post("login", this.loginForm)
          .then((res) => {
            if (res.data.code === 0) {
              this.$toast(res.data.msg);
            } else {
              // 登录成功
              this.$store.commit("user/setUser", res.data.data);
              this.$auth.setAuthorization(res.data.data.session_id)
              this.$toast(res.data.msg);
              this.$router.replace({ name: "user" });
            }
          })
          .catch(() => {
            this.$toast("登录失败");
          });
      }
    },
    register() {
      this.$router.push({ name: "register" });
    },
  },
};
</script>